package com.fr.general;

import com.fr.general.http.HttpClient;
import com.fr.json.JSONException;
import com.fr.json.JSONObject;
import com.fr.stable.Branch;
import com.fr.stable.ProductConstants;
import com.fr.stable.StringUtils;
import com.fr.stable.bridge.StableFactory;
import com.fr.stable.fun.SiteTransformer;
import com.fr.stable.plugin.ExtraClassManagerProvider;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public class SiteCenter
{
  private static int TIME_OUT = 5000;
  private static final String[] SITE_SOURCE = { "http://support.finereport.com/site", "http://support.finereport.com:54321/site" };
  private static SiteCenter ourInstance;
  private Map<String, String> map = new HashMap();
  private boolean loadSuccess = false;

  public static SiteCenter getInstance()
  {
    if (ourInstance != null)
      return ourInstance;
    synchronized (SiteCenter.class)
    {
      if (ourInstance == null)
        ourInstance = new SiteCenter();
      return ourInstance;
    }
  }

  private SiteCenter()
  {
    prepare();
  }

  private void prepare()
  {
    if (this.loadSuccess)
      return;
    new Thread()
    {
      public void run()
      {
        SiteCenter.this.loadConfig();
      }
    }
    .start();
  }

  private void loadConfig()
  {
//    for (String str1 : SITE_SOURCE)
//      try
//      {
//        HttpClient localHttpClient = new HttpClient(str1 + "?build=" + GeneralUtils.readBuildNO() + ProductConstants.BRANCH.toHttpQuery());
//        localHttpClient.setTimeout(TIME_OUT);
//        localHttpClient.asGet();
//        if (localHttpClient.isServerAlive())
//        {
//          InputStream localInputStream = localHttpClient.getResponseStream();
//          String str2 = IOUtils.inputStream2String(localInputStream, "UTF-8");
//          if (StringUtils.isNotBlank(str2))
//          {
//            JSONObject localJSONObject = new JSONObject(str2);
//            Iterator localIterator = localJSONObject.keys();
//            while (localIterator.hasNext())
//            {
//              String str3 = (String)localIterator.next();
//              this.map.put(str3, localJSONObject.optString(str3));
//            }
//            this.loadSuccess = true;
//            break;
//          }
//        }
//      }
//      catch (IOException localIOException)
//      {
//        FRLogger.getLogger().error("Cannot connect to site server:" + str1);
//      }
//      catch (JSONException localJSONException)
//      {
//        FRLogger.getLogger().error("Cannot connect to site server:" + str1);
//      }
  }

  public String acquireUrlByKind(String paramString1, String paramString2)
  {
    String str = acquireUrlByKind(paramString1);
    if (StringUtils.isEmpty(str))
      return paramString2;
    return str;
  }

  public String acquireUrlByKind(String paramString)
  {
    ExtraClassManagerProvider localExtraClassManagerProvider = StableFactory.getExtraClassManager();
    if (localExtraClassManagerProvider != null)
    {
      Set localSet = localExtraClassManagerProvider.getArray("SiteTransformer");
      Iterator localIterator = localSet.iterator();
      while (localIterator.hasNext())
      {
        SiteTransformer localSiteTransformer = (SiteTransformer)localIterator.next();
        if (localSiteTransformer.match(paramString))
          return localSiteTransformer.transform();
      }
    }
    if (!this.loadSuccess)
      new Thread()
      {
        public void run()
        {
          SiteCenter.this.loadConfig();
        }
      }
      .start();
    return (String)this.map.get(paramString);
  }
}